<?php

/* Filename: plugin/base/cms/stage1/connections.php
 *
 * This file contains the datastructures used for managing database connections.
 * The current connection used is stored in the SGDB cookie
 *
 */
?>

<?php

class Connection {

    var $connector;
    var $path;
    var $username;
    var $password;
    var $host;
    var $dbname;

    public function getConnector() {
        return $this->connector;
    }

    public function setConnector($connector) {
        $this->connector = $connector;
    }

    public function getPath() {
        return $this->path;
    }

    private function setPath($path) {
        $this->path = $path;
    }

    public function getUsername() {
        return $this->username;
    }

    private function setUsername($username) {
        $this->username = $username;
    }

    public function getPassword() {
        return $this->password;
    }

    private function setPassword($password) {
        $this->password = $password;
    }

    public function getHost() {
        return $this->host;
    }

    private function setHost($host) {
        $this->host = $host;
    }

    public function getName() {
        return $this->dbname;
    }

    private function setName($dbname) {
        $this->dbname = $dbname;
    }

    public function cMySQL($h, $d, $u, $p) {
        //First we test the connection
        try {
            echo 'hahaaa';
            $dbh = new PDO("mysql:host=$h;dbname=$d", $u, $p);
            echo 'hgh';
            //Set the vars
            $this->setConnector('mysql');
            $this->setHost($h);
            $this->setName($d);
            $this->setUsername($u);
            $this->setPassword($p);
            return 'ok';
        }
        //If connection failed
        catch (PDOException $e) {
            return $e->getMessage();
        }
    }

    public function cSQLite($p) {
        $this->setConnector('sqllite');
        $this->setPath($p);
        return 'ok';
    }

}

class ConnectionPool {

    private $pool = array();

    public function setupOK() {
        return count($this->pool);
    }

    public function addConnection($connection) {
        $i = array_push($this->pool, $connection);
        if (!isset($_COOKIE['testcookie'])) {
            selectConnection($i - 1);
        }
    }

    public function hasConnection($index) {
        return array_key_exists($index, $this->pool);
    }

    public function useConnection($pindex) {

        //echo "index search: " . $pindex . "xx";
        return $this->pool[$pindex];
    }

    public function getCurrent() {
        
        $cval = $_COOKIE['testcookie'];
        return $this->pool[$cval];
    }

}

function selectConnection($c) {
    echo "setting cookie:" . $_SESSION['rootdomain'];
    setcookie('testcookie', $c, time() + 3600 * 24 * 365, '/', $_SESSION['rootdomain']);
}

function createDatabase($connection) {

    $pdocreate = getPDO($connection);
    $src = $_SESSION['root'] . '/plugin';
    if ($handle = opendir($src)) {
        echo "creating database";
        while (false !== ($file = readdir($handle))) {
            if ($file != "." && $file != ".." && $file[0] != ".") {
                $concat = $src;
                $concat .= '/';
                $concat .= $file;
                if (is_dir($concat)) {
                    $concat .= '/sql/create.php';
                    include $concat;
                }
            }
        }
        closedir($handle);
    } else {
        echo "Problems with: " . $src;
    }

    closePDO($pdocreate);
}

function getConnection() {
    include $_SESSION['root'] . '/plugin/base/cms/stage1/connectors.php';  
    $retc = $cpool->getCurrent();
    return getPDO($retc);
}

function getPDO($connection) {
    $t = $connection->getConnector();

    switch ($t) {
        case 'mysql': {
                $h = $connection->getHost();
                $d = $connection->getName();
                $u = $connection->getUsername();
                $p = $connection->getPassword();

                $pdodb = new PDO("mysql:host=$h;dbname=$d", $u, $p);
                return $pdodb;
            }
    }
}

function closePDO($pdoc) {
    $pdoc = null;
}

function writeToConnectors($wc) {
    //First extract data from connection
    $connectors = $_SESSION['root'] . '/plugin/base/cms/stage1/connectors.php';
    chmod($connectors, 0666);
    echo 'writing: ', $connectors, '<BR />';


    $p = $wc->getPath();
    $h = $wc->getHost();
    $d = $wc->getName();
    $u = $wc->getUsername();
    $p = $wc->getPassword();
    $t = $wc->getConnector();

    $fh = fopen($connectors, 'a+') or die("can't open file");
    $stringData = "<?php\n";
    fwrite($fh, $stringData);

    switch ($t) {
        case 'mysql': {

                $initconnector = "\$connector= new Connection();\n";
                fwrite($fh, $initconnector);
                $mysqlconnect = "\$connector->cMySQL('$h', '$d', '$u', '$p');\n";
                fwrite($fh, $mysqlconnect);
            }
    }

    $poolwrite = "\$cpool->addConnection(\$connector);\n";
    fwrite($fh, $poolwrite);
    $stringData = "?>\n";
    fwrite($fh, $stringData);
    fclose($fh);

    echo "connections found";
}
?>